package com.simple.chiralium.upms.biz.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.simple.chiralium.common.core.util.Result;
import com.simple.chiralium.upms.api.dto.UserInfo;
import com.simple.chiralium.upms.api.entity.SysMenu;
import com.simple.chiralium.upms.api.entity.SysRole;
import com.simple.chiralium.upms.api.entity.SysUser;
import com.simple.chiralium.upms.biz.mapper.SysUserMapper;
import com.simple.chiralium.upms.biz.mapper.SysUserRoleMapper;
import com.simple.chiralium.upms.biz.service.SysMenuService;
import com.simple.chiralium.upms.biz.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 系统用户服务impl
 *
 * @author SimpleZzz
 * @date 2021/12/05
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    /**
     * 用户mapper
     */
    private final SysUserMapper userMapper;

    /**
     * 用户角色关联mapper
     */
    private final SysUserRoleMapper userRoleMapper;

    /**
     * 菜单服务
     */
    private final SysMenuService menuService;

    /**
     * 添加用户
     *
     * @param user 用户
     * @return {@link Result}<{@link String}>
     */
    @Override
    public Result<String> addUser(SysUser user) {
        this.save(user);
        return Result.ok();
    }

    /**
     * 更新用户
     *
     * @param user 用户
     * @return {@link Result}<{@link String}>
     */
    @Override
    public Result<String> updateUserById(SysUser user) {
        boolean updated = this.updateById(user);
        return updated ? Result.ok("更新成功") : Result.failed("更新失败");
    }

    /**
     * 删除通过id
     *
     * @param id id
     * @return {@link Result}<{@link String}>
     */
    @Override
    public Result<String> deleteUserById(String id) {
        boolean removed = this.removeById(id);
        return removed ? Result.ok("删除成功") : Result.failed("删除失败");
    }

    /**
     * 获取用户信息
     *
     * @param sysUser 系统用户
     * @return {@link UserInfo}
     */
    @Override
    public UserInfo getUserInfo(SysUser sysUser) {
        UserInfo userInfo = new UserInfo();
        List<SysRole> roleList = userRoleMapper.listRolesByUserId(sysUser.getPkId());
        userInfo.setSysUser(sysUser);
        userInfo.setRoleList(roleList);
        List<Long> roleIdList = roleList.stream().map(SysRole::getPkId).collect(Collectors.toList());
        userInfo.setRoleIds(ArrayUtil.toArray(roleIdList, Long.class));
        Set<String> permissions = roleIdList.stream().map(menuService::findMenuByRoleId).flatMap(Collection::stream)
                .map(SysMenu::getPermission)
                .filter(StrUtil::isNotBlank)
                .collect(Collectors.toSet());
        userInfo.setPermissions(ArrayUtil.toArray(permissions, String.class));

        return userInfo;
    }
}
